home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static char sccsid[] = "@(#)rmail.c 4.4 (Berkeley) 8/11/83";
- #endif
-
- /*
- ** RMAIL -- UUCP mail server.
- **
- ** This program reads the >From ... remote from ... lines that
- ** UUCP is so fond of and turns them into something reasonable.
- ** It calls sendmail giving it a -f option built from these
- ** lines.
- **
- ** Modified to set the sender's hostname (deduced from first
- ** "remote from host" line) and convert the final "user" part
- ** from an address with '@' and '%' (which mmdf is so fond of)
- ** in to a 'pure' uucp address. -Jim Crammond, (hwcs!jim) 29/11/84
- */
-
- # include <stdio.h>
- # include <sysexits.h>
-
- typedef char bool;
- #define TRUE 1
- #define FALSE 0
-
- extern FILE *popen();
- extern char *index();
- extern char *rindex();
- char *perc_to_uucp();
-
- bool Debug;
-
- # define MAILER "/usr/lib/sendmail"
-
- main(argc, argv)
- char **argv;
- {
- FILE *out; /* output to sendmail */
- char lbuf[512]; /* one line of the message */
- char from[512]; /* accumulated path of sender */
- char ufrom[128]; /* user on remote system */
- char sys[64]; /* a system in path */
- char sysname[64]; /* system received from */
- char cmd[2000];
- register char *cp;
- register char *uf; /* ptr into ufrom */
- int linecount;
- int i;
-
- # ifdef DEBUG
- if (argc > 1 && strcmp(argv[1], "-T") == 0)
- {
- Debug = TRUE;
- argc--;
- argv++;
- }
- # endif DEBUG
-
- if (argc < 2)
- {
- fprintf(stderr, "Usage: rmail user ...\n");
- exit(EX_USAGE);
- }
-
- (void) strcpy(from, "");
- (void) strcpy(sysname, "");
- (void) strcpy(ufrom, "/dev/null");
-
- linecount = 0;
- while (fgets(lbuf, sizeof lbuf, stdin) != NULL)
- {
- if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
- break;
- linecount++;
- (void) sscanf(lbuf, "%*s %s", ufrom);
- cp = lbuf;
- uf = ufrom;
-
- while ((cp = index(cp, 'r')) != NULL)
- {
- #ifdef DEBUG
- if (Debug)
- printf("cp='%s'\n", cp);
- #endif
- if (sscanf(cp, "remote from %s", sys) == 1)
- { (void) strcat(from, sys);
- (void) strcat(from, "!");
- if (linecount == 1)
- (void) strcpy(sysname, sys);
- break;
- }
- cp++;
- }
- #ifdef DEBUG
- if (Debug)
- printf("ufrom='%s', sys='%s', from now '%s'\n", uf, sys, from);
- #endif
- }
-
- /*
- * check for percent style addresses in user field
- */
- if (index(uf, '@') != NULL || index(uf, '%') != NULL)
- uf = perc_to_uucp(uf);
-
- /*
- * if this is a new style "From domain!user .. remote from system"
- * header then don't prepend the system name to the from person
- */
- if (linecount == 1 && (cp = index(uf, '!')) != NULL)
- { char *p = index(uf, '.');
- if (p != NULL && p < cp)
- (void) strcpy(from, uf);
- else
- (void) strcat(from, uf);
- }
- else
- (void) strcat(from, uf);
-
- (void) sprintf(cmd, "%s -ee -oi -f%s", MAILER, from);
- if (*sysname != '\0')
- { (void) strcat(cmd, " -oMs");
- (void) strcat(cmd, sysname);
- }
-
- while (*++argv != NULL)
- {
- (void) strcat(cmd, " '");
- if (**argv == '(')
- (void) strncat(cmd, *argv + 1, strlen(*argv) - 2);
- else
- (void) strcat(cmd, *argv);
- (void) strcat(cmd, "'");
- }
- #ifdef DEBUG
- if (Debug)
- printf("cmd='%s'\n", cmd);
- #endif
- out = popen(cmd, "w");
- fputs(lbuf, out);
- while (fgets(lbuf, sizeof lbuf, stdin))
- fputs(lbuf, out);
- i = pclose(out);
- if ((i & 0377) != 0)
- {
- fprintf(stderr, "pclose: status 0%o\n", i);
- exit(EX_OSERR);
- }
-
- exit((i >> 8) & 0377);
- }
-
-
- /*
- ** PERC_TO_UUCP -- converts an address in Percent style into uucp style
- **
- ** e.g. user%c.bitnet%b.arpa@a.uucp -> a.uucp!b.arpa!c.bitnet!user
- */
- char *
- perc_to_uucp(addr)
- char *addr;
- {
- static char buf[512];
- char *bp = buf;
- char *p;
-
- #ifdef DEBUG
- if (Debug)
- printf("perc_to_uucp(%s) ", addr);
- #endif
-
- while ((p = rindex(addr,'@')) != NULL || (p = rindex(addr,'%')) != NULL)
- {
- *p++ = '\0';
- while (*p)
- *bp++ = *p++;
-
- *bp++ = '!';
- }
-
- strcpy(bp, addr);
- #ifdef DEBUG
- printf("returns %s\n", buf);
- #endif
- return(buf);
- }
-